<?php

/**
 * This is the model class for table "tbl_instrument".
 *
 * The followings are the available columns in table 'tbl_instrument':
 * @property integer $instrument_id
 * @property string $title
 * @property string $model
 * @property string $specification
 * @property integer $price
 * @property string $manufacturer
 * @property string $brand
 * @property string $production_date
 * @property string $purchase_date
 * @property string $origin
 * @property string $subordinate_units
 * @property string $contacts
 * @property string $tel
 * @property string $email
 * @property string $place
 * @property string $description
 * @property integer $resources_id
 * @property integer $stock
 * @property integer $status
 */
class Instrument extends CActiveRecord
{
    const STATUS_ACTIVE = 1;
    const STATUS_DEACTIVE = 0;


	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Instrument the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_instrument';
    }

    public function getEnabledLable()
    {
        return $this->status == self::STATUS_ACTIVE ? '可预约' : '不可预约';
    }
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, contacts', 'required'),
			array('price, resources_id, stock,total_stock, status', 'numerical', 'integerOnly'=>true),
			array('title, model, specification, manufacturer, brand, origin, subordinate_units, contacts, place', 'length', 'max'=>200),
			array('tel, email', 'length', 'max'=>100),
			array('production_date, purchase_date, description', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('instrument_id, title, model, specification, price, manufacturer, brand, production_date, purchase_date, origin, subordinate_units, contacts, tel, email, place, description, resources_id, stock, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'resources'=>array(self::BELONGS_TO,'Resources','resources_id'),
            'isEnabledDate'=>array(self::HAS_MANY,'InstrumentDate','instrument_id','condition'=>'enabled = '.InstrumentDate::ENABLED_TRUE),
            'unEnabledDate'=>array(self::HAS_MANY,'InstrumentDate','instrument_id','condition'=>'enabled = '.InstrumentDate::ENABLED_FALSE),
            'instrumentElems'=>array(self::HAS_MANY,'InstrumentElem','instrument_id','condition'=>'status = '.InstrumentElem::STATUS_ACTIVE),
		);
	}
    public function getReserveList()
    {
        $resultArray = array();
        $list = $this->instrumentElems;
        foreach($list as $elem)
        {
            $rlist = $elem->instrumentReserves;
            foreach($rlist as $value)
            {
                array_push($resultArray, $value);
            }
        }
        return $resultArray;
    }
    /**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'instrument_id' => '大型仪器类ID',
			'title' => '仪器名称',
			'model' => '型号',
			'specification' => '规格',
			'price' => '单价',
			'manufacturer' => '生产厂家',
			'brand' => '品牌',
			'production_date' => '生产日期',
			'purchase_date' => '购买日期',
			'origin' => '产地',
			'subordinate_units' => '所属单位',
			'contacts' => '联系人',
			'tel' => '联系电话',
			'email' => '联系邮箱',
			'place' => '所在地点',
			'description' => '功能描述',
			'resources_id' => '图片资源',
			'stock' => '库存',
			'total_stock' => '总数',
			'status' => '状态',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('instrument_id',$this->instrument_id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('model',$this->model,true);
		$criteria->compare('specification',$this->specification,true);
		$criteria->compare('price',$this->price);
		$criteria->compare('manufacturer',$this->manufacturer,true);
		$criteria->compare('brand',$this->brand,true);
		$criteria->compare('production_date',$this->production_date,true);
		$criteria->compare('purchase_date',$this->purchase_date,true);
		$criteria->compare('origin',$this->origin,true);
		$criteria->compare('subordinate_units',$this->subordinate_units,true);
		$criteria->compare('contacts',$this->contacts,true);
		$criteria->compare('tel',$this->tel,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('place',$this->place,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('resources_id',$this->resources_id);
		$criteria->compare('stock',$this->stock);
		$criteria->compare('total_stock',$this->total_stock);
		$criteria->compare('status',$this->status);

		$criteria->order = 'instrument_id DESC';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
            'pagination'=>array(
                'pageSize'=>20,
            ),
		));
	}
}
